package com.cleamy.demo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;
    private static String resources = "mybatis-config.xml";

    private MybatisUtil() {}

    static {
        Reader reader = null;

        try {
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            System.out.println("读取");
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"dev");
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }


    public static void setResources(String resources) {
        MybatisUtil.resources = resources;
    }
}
